﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Xml.Linq;

namespace Framework
{
    public partial class Site : System.Web.UI.MasterPage
    {
        public DGSFrameworkEntities entity = new DGSFrameworkEntities();

        public List<udv_Categories> allCate = new List<udv_Categories>();
        public List<udv_Articles> allArt = new List<udv_Articles>();
        public int ct;
        public XElement xEInfomationConfigWebsite;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCacheCategory();
                LoadMenu();
                LoadTopMenu();
                GetTop1Article();
            }
        }
        public void SetLanguage(object sender, EventArgs e)
        {
            if (Globals.CheckRewriteUrl() == false)
            {
                Session["language"] = ((ImageButton)sender).Attributes["rel"].ToString();
                Response.Redirect(Request.RawUrl);
            }
            else
            {
                Session["language"] = ((ImageButton)sender).Attributes["rel"].ToString();
                if (Request.Url.LocalPath.ToLower().Contains("default.aspx"))
                {
                    Response.Redirect(ResolveUrl("~/"));
                }
                else
                {
                    //Response.Redirect(ResolveUrl("~/"));
                    //Response.Redirect(Request.ServerVariables["HTTP_X_REWRITE_URL"]);
                    if (Request.ServerVariables["HTTP_X_REWRITE_URL"] != null)
                        Response.Redirect(Request.ServerVariables["HTTP_X_REWRITE_URL"]);
                    else
                        Response.Redirect(Request.RawUrl);
                }
            }
        }

        public void LoadMenu()
        {
            var parentMenu = allCate
                .Where(ca => (ca.ParentID == ConfigPaths.Category_Article_ID || ca.ParentID == ConfigPaths.Category_Product_ID) && ca.IsMenuBottom == true)
                .OrderBy(ca => ca.DisplayOrder)
                .ThenByDescending(ca => ca.AddedDate)
                .ToList();
            rptMenu.DataSource = parentMenu;
            rptMenu.DataBind();
        }
        public void LoadTopMenu()
        {
            var topMenu = allCate.Where(ca => (ca.ParentID == ConfigPaths.Category_Article_ID || ca.ParentID == ConfigPaths.Category_Product_ID) && ca.IsActive == true)
                .OrderBy(ca => ca.DisplayOrder)
                .ThenByDescending(ca => ca.AddedDate)
                .ToList();
            rptTopMenu.DataSource = topMenu;
            rptTopMenu.DataBind();
        }
        protected void rptMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal lblsubMenu = (Literal)e.Item.FindControl("lblSubMenu");
            var parentMenu = (udv_Categories)e.Item.DataItem;
            lblsubMenu.Text = LoopCateMenu(ref allCate, parentMenu.CateID, Globals.GetUrlAspx(parentMenu.UrlRewrite, true));
        }
        public string LoopCateMenu(ref List<udv_Categories> listCate, int cateID, string url)
        {
            var cateChild = listCate.Where(c => c.ParentID == cateID && c.IsActive == true).OrderBy(c => c.DisplayOrder).ThenByDescending(c => c.AddedDate).ToList();
            string menu = "";
            if (cateChild.Count() == 0)
            {
                return menu;
            }
            menu = "<ul class='sub-menu'>";
            foreach (var item in cateChild)
            {
                menu += "<li><a id='aMenu" + item.CateID + "' href='" + ResolveUrl("~/" + (Globals.GetUrl(item.UrlRewrite, url + item.CateID))) + "' title='" + Globals.GetLanguage(item.Title, item.Title_en, true) + "'>" + Globals.GetLanguage(item.Title, item.Title_en, true) + "</a>";
                menu += LoopCateMenu(ref listCate, item.CateID, url);
                menu += "</li>";
            }
            menu += "</ul>";
            return menu;
        }
        public void LoadCacheCategory()
        {
            if (Cache["Category"] == null)
            {
                Cache["Category"] = allCate = entity.udv_Categories.ToList();
            }
            else
            {
                allCate = (List<udv_Categories>)Cache["Category"];
            }
        }
        public int LoadmenuParenid(int pr)
        {
            var bd = allCate.Where(c => c.CateID == pr).FirstOrDefault();
            ct = bd.CateID;
            return int.Parse(bd.ParentID.ToString());
        }
        public void GetTop1Article()
        {
            try
            {
                var top1 = entity.udv_Articles.Where(a => a.udv_Categories.IsTop == true).OrderBy(a => a.DisplayOrder).ThenByDescending(a => a.AddedDate).Take(1);
                rtpCourse.DataSource = top1;
                rtpCourse.DataBind();
            }
            catch (Exception)
            {

                throw;
            }
        }

    }
}